"use strict";
/* Controllers */
app.controller("ctiAddController", ['$scope', '$q', '$location', '$rootScope', '$routeParams', 'ctiAddService', function ($scope, $q, $location, $rootScope, $routeParams, ctiAddService) {

    var vm = $scope.vm = {};
    vm.p = {};

    vm.p.type = $routeParams.type; // 如果id!=0为更新
    if(vm.p.type=='rg') {
        vm.p.resolvegroupId = $routeParams.id;
        vm.p.ctiId = '0';
    }else {
        vm.p.ctiId = $routeParams.id;
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listResolveGroup = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (res) {
            if (res.success && res.data) {
                vm.listResolveGroup = res.data;
                $scope.rgMaps = {}
                for (var index in vm.listResolveGroup) {
                    $scope.rgMaps[vm.listResolveGroup[index].resolvegroupId] = vm.listResolveGroup[index]
                }
                console.log("rgMaps" + $scope.rgMaps)
            } else {
                alert(res.msg);
            }
        };
        return ctiAddService.listResolveGroup(params, procFn, errorFn);
    };

    $scope.addCti = function () {
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！")
            } else {
                alert(response.msg);
            }
        };
        ctiAddService.addCti(params, procFn, errorFn);
    };

    $scope.getCti = function () {
        var vm = $scope.vm;
        var params = {
            ctiId: vm.p.ctiId
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                vm.p = (response.data);
            } else {
                alert(response.msg);
            }
        };
        ctiAddService.getCti(params, procFn, errorFn);
    };

    if(vm.p.ctiId!=0) {
        console.log(vm.p.ctiId);
        $q.all([$scope.listResolveGroup()]).then(function () {
            $scope.getCti();
        })
    }else {
        $scope.listResolveGroup();
    }
}]);


/* Services */
app.service('ctiAddService', ['ajaxService', function (ajaxService) {

    this.listResolveGroup = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/onduty-app-service/v1/resolvegroup/listResolvegroup.do", successFunction, errorFunction);
    };

    this.addCti = function (ctis, successFunction, errorFunction) {
        ajaxService.AjaxPost(ctis, "/star/api/onduty-app-service/v1/cti/addCti.do", successFunction, errorFunction);
    };

    this.getCti = function (ctis, successFunction, errorFunction) {
        ajaxService.AjaxPost(ctis, "/star/api/onduty-app-service/v1/cti/getCti.do", successFunction, errorFunction);
    };

}]);

